﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;


namespace QuanLyKhoHangVatTu
{
    public partial class NhomvatTuHangHoa : DevExpress.XtraEditors.XtraForm
    {
        NhomVatTu_BUS nvt_bus = new NhomVatTu_BUS();
        NhomVatTu_PB nvt_pb = new NhomVatTu_PB();
        public NhomvatTuHangHoa()
        {
            InitializeComponent();
        }

        public NhomvatTuHangHoa(string ma, string ten)
        {
            InitializeComponent();

            txtMa.Text = ma;
            txtTen.Text = ten;
            txtMa.Properties.ReadOnly = true;
        }

        private void ThemNhomvatTuHangHoa_Load(object sender, EventArgs e)
        {
            if (Flag_PB.ThemSua == 1)
            {
                txtMa.ResetText();
                txtTen.ResetText();
            }
            if (Flag_PB.ThemSua == 2)
            {
                txtMa.Text = NhomVatTu_PB.MaNhomVatTu;
                txtMa.Properties.ReadOnly = true;
                txtTen.Text = NhomVatTu_PB.TenNhomVatTu;
            }
        }
        private bool ChecksNull()
        {
            if (txtMa.Text == string.Empty)
            {
                txtMa.ErrorText = "Đơn vị tính không được trống";
                return false;
            }

            if (nvt_bus.Exists(txtMa.Text))
            {
                if (Flag_PB.Sua == true)
                {
                    Flag_PB.Sua = false;
                    return true;

                }
                txtMa.ErrorText = "Mã trùng";
                return false;
            }

            return true;
        }
        //Nut Luu_Them
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            txtMa.Properties.ReadOnly = false;
            GetValues();
            if (ChecksNull())
            {
                //if (Flag_PB.ThemSua == 1)
                //{
                try
                {
                    nvt_bus.ThemSuaNhomVatTu(nvt_pb);
                    txtMa.ResetText();
                    txtTen.ResetText();
                    if (this.Tag != null)
                    {
                        IChildForm iChildForm = this.Tag as IChildForm;
                        if (iChildForm != null)
                            iChildForm.Nap();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains(" data would be truncated"))
                        XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                //}
                //if (Flag_PB.ThemSua == 2)
                //{
                //    txtMa.Properties.ReadOnly = false;
                //    nvt_bus.SuaNhomVatTu(nvt_pb);
                //    Flag_PB.ThemSua = 1;
                //}
                
            }
            //else
            //{
            //    MessageBox.Show("Mã/Tên không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return;
            //}
            
        }
        public NhomVatTu_PB GetValues()
        {


            NhomVatTu_PB.MaNhomVatTu = txtMa.Text;
            NhomVatTu_PB.TenNhomVatTu = txtTen.Text;
            return nvt_pb;
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }
        //Nut them
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            GetValues();
            if (ChecksNull())
            {
                    try
                    {
                        nvt_bus.ThemSuaNhomVatTu(nvt_pb);
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains(" data would be truncated"))
                            XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                //if (Flag_PB.ThemSua == 2)
                //{
                //    nvt_bus.SuaNhomVatTu(nvt_pb);
                //}
                //DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                //MessageBox.Show("Mã/Tên không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}